package com.bbva.kyfb.kyfbjpatlsb.dominiotlsb;

import java.security.SecureRandom;
import java.sql.Blob;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.dod.RooDataOnDemand;
import org.springframework.stereotype.Component;

/**
 * The Class TbduefesDataOnDemand.
 */
@Component
@Configurable
@RooDataOnDemand(entity = Tbduefes.class)
public class TbduefesDataOnDemand {

	/** The rnd. */
	private Random rnd = new SecureRandom();

	/** The data. */
	private List<Tbduefes> data;

	/**
	 * Gets the new transient tbduefes.
	 *
	 * @param index the index
	 * @return the new transient tbduefes
	 */
	public Tbduefes getNewTransientTbduefes(int index) {
        Tbduefes obj = new Tbduefes();
        setEmbeddedIdClass(obj, index);
        setAudFmodific(obj, index);
        setAudUsuario(obj, index);
        setDesServpau(obj, index);
        setDesSesion(obj, index);
        setEstEstadenv(obj, index);
        setFecEnviosms(obj, index);
        setTimEnviosms(obj, index);
        setXtiTipenv2(obj, index);
        return obj;
    }

	/**
	 * Sets the embedded id class.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setEmbeddedIdClass(Tbduefes obj, int index) {
        TbduefesPK embeddedIdClass = new TbduefesPK(new Integer(index).shortValue(), new Integer(index).shortValue(), "codLogonapl_" + index, new Integer(index).shortValue(), new Integer(index).shortValue(), new Integer(index).shortValue(), "codUsu_" + index, "codSespau_" + index);
        obj.setId(embeddedIdClass);
    }

	/**
	 * Sets the aud fmodific.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setAudFmodific(Tbduefes obj, int index) {
        Date audFmodific = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setAudFmodific(audFmodific);
    }

	/**
	 * Sets the aud usuario.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setAudUsuario(Tbduefes obj, int index) {
        String audUsuario = "audUsu_" + index;
        if (audUsuario.length() > 8) {
            audUsuario = audUsuario.substring(0, 8);
        }
        obj.setAudUsuario(audUsuario);
    }

	/**
	 * Sets the des servpau.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setDesServpau(Tbduefes obj, int index) {
        String desServpau = "desServpau_" + index;
        if (desServpau.length() > 254) {
            desServpau = desServpau.substring(0, 254);
        }
        obj.setDesServpau(desServpau);
    }

	/**
	 * Sets the des sesion.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setDesSesion(Tbduefes obj, int index) {
        Blob desSesion = null;
        obj.setDesSesion(desSesion);
    }

	/**
	 * Sets the est estadenv.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setEstEstadenv(Tbduefes obj, int index) {
        Short estEstadenv = new Integer(index).shortValue();
        obj.setEstEstadenv(estEstadenv);
    }

	/**
	 * Sets the fec enviosms.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setFecEnviosms(Tbduefes obj, int index) {
        Date fecEnviosms = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setFecEnviosms(fecEnviosms);
    }

	/**
	 * Sets the tim enviosms.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setTimEnviosms(Tbduefes obj, int index) {
        Date timEnviosms = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setTimEnviosms(timEnviosms);
    }

	/**
	 * Sets the xti tipenv2.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setXtiTipenv2(Tbduefes obj, int index) {
        Character xtiTipenv2 = new Character('N');
        obj.setXtiTipenv2(xtiTipenv2);
    }

	/**
	 * Gets the specific tbduefes.
	 *
	 * @param index the index
	 * @return the specific tbduefes
	 */
	public Tbduefes getSpecificTbduefes(int index) {
        init();
        if (index < 0) index = 0;
        if (index > (data.size() - 1)) index = data.size() - 1;
        Tbduefes obj = data.get(index);
        return Tbduefes.findTbduefes(obj.getId());
    }

	/**
	 * Gets the random tbduefes.
	 *
	 * @return the random tbduefes
	 */
	public Tbduefes getRandomTbduefes() {
        init();
        Tbduefes obj = data.get(rnd.nextInt(data.size()));
        return Tbduefes.findTbduefes(obj.getId());
    }

	/**
	 * Modify tbduefes.
	 *
	 * @param obj the obj
	 * @return true, if successful
	 */
	public boolean modifyTbduefes(Tbduefes obj) {
        return false;
    }

	/**
	 * Inits the.
	 */
	public void init() {
        data = Tbduefes.findTbduefesEntries(0, 10);
        if (data == null) throw new IllegalStateException("Find entries implementation for 'Tbduefes' illegally returned null");
        if (!data.isEmpty()) {
            return;
        }
        
        data = new ArrayList<com.bbva.kyfb.kyfbjpatlsb.dominiotlsb.Tbduefes>();
        for (int i = 0; i < 10; i++) {
            Tbduefes obj = getNewTransientTbduefes(i);
            try {
                obj.persist();
            } catch (ConstraintViolationException e) {
                StringBuilder msg = new StringBuilder();
                for (Iterator<ConstraintViolation<?>> it = e.getConstraintViolations().iterator(); it.hasNext();) {
                    ConstraintViolation<?> cv = it.next();
                    msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage()).append("=").append(cv.getInvalidValue()).append("]");
                }
                throw new RuntimeException(msg.toString(), e);
            }
            obj.flush();
            data.add(obj);
        }
    }
}
